<?php

namespace App\Jobs\Controller;

use App\Consts\GlobalCommentConst;
use App\Consts\GlobalConst;
use App\Consts\GlobalCounterConst;
use App\Jobs\BaseJob;

class CommentJob extends BaseJob
{
    /**
     * 评论删除后，该评论如果没有正常的回复
     *  1. 评论的主体的评论数 - 1
     *  2. is_view = 0【不可见】
     * @param array $commentInfo ['comment_id' ='', 'target_id' =>'', 'target_type' => '']
     * @return bool
     */
    public function changeStatus($commentInfo)
    {
        $commentInfo = $commentInfo['msgContent'];
        $haveNormalReply = repository()->ReplyRepository->judgeCommentHaveNormalReply($commentInfo['comment_id']);
        if (!$haveNormalReply) {
            service()->CommentService->_editCount($commentInfo['target_id'], $commentInfo['target_type'], GlobalCounterConst::DECREASE);

            $comment = repository()->CommentRepository->getComment($commentInfo['comment_id']);
            if ($comment && $comment->is_deleted == GlobalConst::IS_DELETED) {
                repository()->CommentRepository->editComment([
                    'comment_id' => $commentInfo['comment_id'],
                    'is_view' => GlobalCommentConst::IS_NOT_VIEW
                ]);

                repository()->CommentRepository->editIndexComment([
                    ['comment_id', $commentInfo['comment_id']],
                    ['type', GlobalConst::TARGET_TYPE_COMMENT],
                ], [
                    'is_view' => GlobalCommentConst::IS_NOT_VIEW
                ]);
            }

        }

        return true;
    }

    /**
     * 评论/回复后修改是否有主体发布者的评论/回复
     * @param array $commentInfo ['comment_id' ='', 'user_id' => '', 'target_id' =>'', 'target_type' => '']
     * @return bool
     */
    public function changeHaveAuthorReply($commentInfo)
    {
        $commentInfo = $commentInfo['msgContent'];
        if ($commentInfo['target_type'] == GlobalConst::TARGET_TYPE_NOTE) {
            $note = repository()->NoteRepository->getNoteByNoteId($commentInfo['target_id']);
            if (!$note) {
                return true;
            }
            if ($note['user_id'] == $commentInfo['user_id']) {
                repository()->CommentRepository->editComment([
                    'comment_id' => $commentInfo['comment_id'],
                    'have_author_reply' => GlobalCommentConst::HAVE_AUTHOR_REPLY,
                ]);
                repository()->CommentRepository->editIndexComment([
                    ['comment_id', $commentInfo['comment_id']],
                    ['type', GlobalConst::TARGET_TYPE_COMMENT],
                ], [
                    'have_author_reply' => GlobalCommentConst::HAVE_AUTHOR_REPLY,
                ]);
            }
        }

        return true;
    }

    /**
     * 点赞/取消点赞后修改<hot> +1/-1
     * @param array $data ['comment_id' => '', 'trend' => '']
     * @return bool
     */
    public function changeHot($data)
    {
        $data = $data['msgContent'];
        $comment = repository()->CommentRepository->getComment($data['comment_id']);
        if ($comment) {
            $hot = $comment->hot + $data['trend'];
            $comment::getInstance()->update(['hot' => $hot]);
        }

        return true;
    }
}
